/// @tags: NetworkFlowwithLowerUpperBound
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>

using namespace std;

namespace BlueQuantum {

int const N = 1e6 + 5, maxm = 1e6 + 5, INF = 0x7f7f7f7f;

int n, m, s, t, s1, t1, s2, t2, sum, ans, e_cnt;
int dep[N], in[N], out[N], G[N];
int heads[N], cur[N];

struct Edge {
  int v, nxt, f;
} e[N * 2];

inline void add(int u, int v, int f) {
  e[++e_cnt].v = v, e[e_cnt].nxt = heads[u], heads[u] = e_cnt, e[e_cnt].f = f;
  e[++e_cnt].v = u, e[e_cnt].nxt = heads[v], heads[v] = e_cnt, e[e_cnt].f = 0;
}

inline bool bfs() {
  memset(dep, 0, sizeof(dep));
  memcpy(cur, heads, sizeof(cur));
  dep[s] = 1;
  queue<int> q;
  q.push(s);
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (int i = heads[u], v; i; i = e[i].nxt) {
      if (dep[v = e[i].v] == 0 && e[i].f) {
        dep[v] = dep[u] + 1;
        if (v == t) return true;
        q.push(v);
      }
    }
  }
  return false;
}

int dinic(int u, int flow) {
  if (u == t) return flow;
  int rest = flow;
  for (int &i = cur[u], v; i; i = e[i].nxt) {
    if (dep[v = e[i].v] == dep[u] + 1 && e[i].f) {
      int k = dinic(v, min(rest, e[i].f));
      e[i].f -= k, e[i ^ 1].f += k;
      rest -= k;
      if (rest == 0) break;
    }
  }
  return flow - rest;
}

inline int getMaxFlow() {
  int res = 0;
  while (bfs()) res += dinic(s, INF);
  return res;
}

inline int main() {
  while (cin >> n >> m, cin.eof() == false) {
    sum = 0, e_cnt = 1;
    memset(heads, 0, sizeof(heads));
    memset(in, 0, sizeof(in));
    memset(out, 0, sizeof(out));
    s1 = n + m + 1, t1 = n + m + 2, s2 = n + m + 3, t2 = n + m + 4;
    s = s2, t = t2;
    for (int i = 1; i <= m; ++i) {
      cin >> G[i];
      in[t1] += G[i], out[n + i] += G[i];
      add(n + i, t1, INF - G[i]);
    }
    for (int i = 1, C, D, T, L, R; i <= n; ++i) {
      cin >> C >> D;
      add(s1, i, D);
      for (int j = 1; j <= C; ++j) {
        cin >> T >> L >> R;
        T++;
        in[n + T] += L, out[i] += L;
        add(i, n + T, R - L);
      }
    }
    for (int i = 1; i <= n + m + 2; ++i) {
      if (in[i] > out[i])
        add(s, i, in[i] - out[i]), sum += in[i] - out[i];
      else
        add(i, t, out[i] - in[i]);
    }
    add(t1, s1, INF);
    if (getMaxFlow() != sum) {
      cout << "-1\n\n";
      continue;
    }
    ans = e[e_cnt].f;
    e[e_cnt].f = e[e_cnt ^ 1].f = 0;
    s = s1, t = t1;
    ans += getMaxFlow();
    cout << ans << "\n\n";
  }
  return 0;
}

}  // namespace BlueQuantum

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
  freopen("/tmp/CodeTmp/testdata.in", "r", stdin);
  freopen("/tmp/CodeTmp/testdata.out", "w", stdout);
#else
  freopen("NetFlowWithLowerUpperBound.in", "r", stdin);
  freopen("NetFlowWithLowerUpperBound.out", "w", stdout);
#endif
#endif

  ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  return BlueQuantum::main();
}
